<?php

namespace app\http\middleware;

use app\admin\model\AdminLog;
use think\response\Json;

/**
 * Class WriteLog
 * @package app\http\middleware
 * Author: LCY<409028735@qq.com>
 * Time: 2021/12/2 16:36
 * Note:写入操作日志
 */
class WriteLog
{
    public function handle($request, \Closure $next)
    {
        $response = $next($request);
        //必须是post请求，并且控制器中没有手动保存日志，这里才需要记录日志
        if($response->getCode() == 200 && $request->isPost() && !$request->hasWriteLog) {
            $data = $response->getData();
            if($response instanceof Json) {
                //执行成功才需要记录日志
                if(isset($data['code']) && $data['code'] == 1) {
                    AdminLog::saveAdminLog();
                }
            }

        }

        return $response;
    }
}
